﻿using habbo.Kernel.Commons;
using habbo.Kernel.Storage.Querys;
using habbo.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace habbo.Habbo.Items
{
    class ItemManager
    {
        private BluedotDictionary<int, Item> Items;

        public ItemManager() { LoadItems(); }

        public void LoadItems()
        {
            Items = new BluedotDictionary<int, Item>();

            int i = 0;
            int j = 0;
            foreach (DataRow Row in SystemApp.MySQLManager.GetObject(new GetAllFurniture()).GetOutput<DataTable>().Rows)
            {
                using (RowAdapter Adapter = new RowAdapter(Row))
                {
                    try
                    {
                        Items.Add(Adapter.PopInt32("id"), new Item(Adapter.PopInt32("id"), Adapter.PopInt32("sprite_id"), Adapter.PopString("public_name"), Adapter.PopString("item_name"), Adapter.PopString("type"), Adapter.PopInt32("width"), Adapter.PopInt32("length"), Adapter.PopDouble("stack_height"), Adapter.PopBoolean("can_stack"), Adapter.PopBoolean("is_walkable"), Adapter.PopBoolean("can_sit"), Adapter.PopBoolean("allow_recycle"), Adapter.PopBoolean("allow_trade"), Adapter.PopBoolean("allow_marketplace_sell"), Adapter.PopBoolean("allow_gift"), Adapter.PopBoolean("allow_inventory_stack"), Adapter.PopString("interaction_type"), Adapter.PopInt32("interaction_modes_count"), Adapter.PopString("vending_ids")));
                        i++;
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Could not load item #" + Adapter.PopInt32("id") + ", please verify that the data is okay.");
                        j++;
                    }
                }
            }

            if (j > 0)
            {
                SystemApp.ConsoleSystem.PrintLine("GLOBAL", j + " item definition(s) could not be loaded.");
            }
        }

        public bool ContainsItem(int Id)
        {
            return Items.ContainsKey(Id);
        }

        public Item GetItem(int Id)
        {
            if (ContainsItem(Id))
            {
                return Items[Id];
            }

            return null;
        }
    }
}
